import { JsonController, Authorized, Body, HeaderParam, Get, Post, UseBefore, UseInterceptor} from 'routing-controllers';
import { getOneUser, addUser } from '@/m_req/User'
import { RequestLog } from '@/middleware/requestLog'
import { Inject, Service } from 'typedi';
import { UserServiceImpl } from '@/impl/userServiceImpl';
import { JsonResult, msgList, res } from '@/utils/jsonResult';
import { User } from '@/m_res/user'
import { Token } from '@/m_req/Token'
import config from '@/config'

@Service()
@JsonController(config.setPrefix("app/user"))
export class UserController {

  @Inject()
  userServiceImpl!: UserServiceImpl;

  @Inject()
  json!: JsonResult<any>;

  /**
 * @api {get} /app/user/info/:id info
 * @apiDescription 获取用户的详细信息
 * @apiName info
 * @apiGroup user
 * @apiParam {number} id 用户的id参数
 * 
 * @apiSuccess {Object} data  用户数据存放在data内部
 * @apiSuccess {Number} data.user_id   用户id
 * @apiSuccess {String} data.user_name   用户名
 * @apiSuccess {Number} data.user_phone   用户手机号
 * @apiSuccess {String} data.user_img  用户头像
 * @apiSuccess {String} data.user_addtime  注册时间

 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "code" : 200,
 *      "msg" : "请求成功"
 *      "data" : {
 *           user_id: 1,
 *           user_name: "bmy",
 *           user_phone: 15056042604,
 *           user_img: "https://www.baidu.com/img/flexible/logo/pc/result@2.png",
 *           user_addtime: "2023-06-03T12:25:16.000Z"
 *      }
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "code": 200,
 *   "msg" : "请求成功"
 *   "data": null
 * }
 * @apiSampleRequest http://127.0.0.1:3000/app/user/info
 */
  @Post('/info')
  @UseBefore(RequestLog)
  @Authorized()
  async getOneUser(@HeaderParam("token") token: Token): Promise<res<User | null>>{
    return await this.userServiceImpl.GetOneUser(token.user_id);
  }

  @Post('/add')
  @UseBefore(RequestLog)
  async add(@Body() params: addUser): Promise<res<null>> {
    return await this.userServiceImpl.addUser(params);
  }

  @Post('/login')
  @UseBefore(RequestLog)
  async Login(@Body() params: addUser): Promise<res<User | null>> {
    console.log("params",params)
    return await this.userServiceImpl.Login(params);
  }
}

